package com.bhs.sansonglogistics.ui.home;

import static android.content.Context.CLIPBOARD_SERVICE;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.OrderGoodsBean;
import com.bhs.sansonglogistics.bean.OrderGoodsDataBean;

import com.bhs.sansonglogistics.bean.UserInfoBean;
import com.bhs.sansonglogistics.bean.event.RefreshOrder;
import com.bhs.sansonglogistics.bean.event.ScreenEvent;
import com.bhs.sansonglogistics.dialog.MyClickListener;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.OrderGoodsAdapter;
import com.bhs.sansonglogistics.ui.order.IncrementalExpensesActivity;
import com.bhs.sansonglogistics.ui.order.OrderDetailsActivity;
import com.bhs.sansonglogistics.ui.order.OrderOperation;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 订单列表
 */
public class OrderGoodsListFragment extends BaseFragment implements NetCallBack {

    private String type;
    private RecyclerView mRvList;

    private OrderGoodsAdapter orderGoodsAdapter;
    private Map<String, String> parameters = new ArrayMap<>();
    private SwipeRefreshLayout refreshLayout;
    private int page = 1;
    private int module;
    private boolean isResume = true;

    private OrderOperation orderOperation;//列表操作按钮

    public OrderGoodsListFragment() {
    }

    public static OrderGoodsListFragment newInstance(String type, int module) {
        OrderGoodsListFragment fragment = new OrderGoodsListFragment();
        Bundle args = new Bundle();
        args.putString("type", type);
        args.putInt("module", module);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString("type");
            module = getArguments().getInt("module");
        }
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_order_goods_list;
    }

    @Override
    public void initView(View root) {
        //注册监听
        EventBus.getDefault().register(this);
        mRvList = root.findViewById(R.id.rv_list);
        orderGoodsAdapter = new OrderGoodsAdapter(module);
        mRvList.setAdapter(orderGoodsAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        orderGoodsAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));
        refreshLayout = root.findViewById(R.id.refreshLayout);
        orderOperation = new OrderOperation((BaseActivity) getActivity());
        //监听 刷新是回调
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

    }

    private void loadData() {
        parameters.put("page", String.valueOf(page));
        if (module == 1) {
            if (!TextUtils.isEmpty(type)) {
                if (type.equals("0")) {
                    parameters.put("pay_status", "0");
                } else if (type.equals("101")) {
                    parameters.put("with_receipt", "2");
                } else {
                    parameters.put("status", type);
                }
            }
            networkRequest(netApi.getOrderList2(parameters), this);
        } else {
            parameters.put("status", type);
            networkRequest(netApi.get_up_waybill_order_list(parameters), this);
        }


    }


    @Override
    public void initData() {
        orderGoodsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                OrderGoodsBean data = (OrderGoodsBean) adapter.getData().get(position);
                startActivity(new Intent(getActivity(), OrderDetailsActivity.class)
                        .putExtra("deliver_sn", data.getDeliver_sn())
                        .putExtra("module", module));
            }
        });

        orderGoodsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        orderOperation.operation(orderGoodsAdapter);
        //按钮操作后刷新列表
        orderOperation.setMyClickListener(new MyClickListener() {
            @Override
            public void onClick(View v) {
                page = 1;
                loadData();
            }
        });
        loadData();
    }


    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 0) {
            refreshLayout.setRefreshing(false);
            OrderGoodsDataBean dataBean = new Gson().fromJson(json, OrderGoodsDataBean.class);
            if (dataBean.isStatus()) {
                if (page == 1) {
                    orderGoodsAdapter.setNewData(dataBean.getData().getList());
                } else {
                    orderGoodsAdapter.loadMoreComplete();
                    orderGoodsAdapter.addData(dataBean.getData().getList());
                }
                if (orderGoodsAdapter.getData().size() >= dataBean.getData().getTotal_count()) {
                    orderGoodsAdapter.loadMoreEnd();
                }
            } else {
                ToastUtil.show(dataBean.getMsg());
            }
        }

    }

    @Subscribe
    public void onEvent(RefreshOrder event) {
        //刷新
        if (event.getType().equals(type) && isResume) {
            page = 1;
            loadData();
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        loadData();
        isResume = true;
    }

    @Override
    public void onPause() {
        super.onPause();
        isResume = false;
    }


    @Subscribe
    public void onEvent(ScreenEvent event) {
        page = 1;
        parameters.put("start_time", event.getStart());
        parameters.put("end_time", event.getEnd());
        parameters.put("line_id", event.getLineId());

        if (event.getStatus().equals("0")) {
            parameters.put("pay_status", "0");
        } else if (event.getStatus().equals("101")) {
            parameters.put("with_receipt", "2");
        } else {
            parameters.put("status", event.getStatus());
        }

        parameters.put("order_type", "");
        parameters.put("deliver_type", "");
        parameters.put("is_yuyue", "");
        if (event.getType().equals("极速")) {
            parameters.put("order_type", "2");
        }
        if (event.getType().equals("指派")) {
            parameters.put("order_type", "1");
        }

        if (event.getType().equals("快运")) {
            parameters.put("deliver_type", "2");
        }
        if (event.getType().equals("普运")) {
            parameters.put("deliver_type", "1");
        }
        if (event.getType().equals("实时单")) {
            parameters.put("is_yuyue", "0");
        }
        if (event.getType().equals("预约单")) {
            parameters.put("is_yuyue", "1");
        }
        loadData();
    }

    @Override
    public void onFailure(int requestCode) {
        if (requestCode == 0) {
            refreshLayout.setRefreshing(false);
            orderGoodsAdapter.loadMoreComplete();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}